package org.wlw.service;

import org.wlw.anno.Resource;
import org.wlw.dao.IUserDao;
import org.wlw.models.User;
import org.wlw.models.WlwSystemException;

import java.util.List;

public class UserService extends BaseService implements IUserService{
    private IUserDao userDao;

    @Resource
    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void add(User t) {
        User tu = this.loadByUsername(t.getUsername());
        if(tu!=null) throw new WlwSystemException("用户已经存在!不能添加!");
        userDao.add(t);
    }

    @Override
    public void update(User t) {
        userDao.update(t);
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

    @Override
    public User load(int id) {
        return userDao.load(id);
    }

    @Override
    public List<User> find() {
        return userDao.find();
    }

    @Override
    public User loadByUsername(String username) {
        return userDao.loadByUsername(username);
    }

    @Override
    public User login(String username, String password) {
        User tu = this.loadByUsername(username);
        if(tu==null) throw new WlwSystemException("用户名或者密码不正确。");
        if(!tu.getPassword().equals(password)) throw new WlwSystemException("用户名或者密码不正确.");
        return tu;
    }
}
